package mytest.controller;

import java.io.IOException;
import java.net.URISyntaxException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import mytest.util.JenkinsServer;
import mytest.util.Response;
import test.testcase.dao.JenkinsDao;
import test.testcase.dao.JobDao;
import test.utils.JsonResult;
import test.service.ServiceJenkins;
import test.service.ServiceProject;
import test.testcase.jenkins;


@Controller @RequestMapping(value = "/jenkins", produces = "application/json; charset=utf-8")
@CrossOrigin
public class Jenkins {
	
	@RequestMapping("/getinfo/{projectid}")
    public @ResponseBody jenkins getinfo(@PathVariable String projectid) {
		jenkins jenkins = ServiceJenkins.getJenkinsInfo(projectid);
		if (jenkins == null) {
			return new jenkins();
		}
		return jenkins;
	}
	
	@RequestMapping("/save/{projectid}")
    public @ResponseBody String save(@PathVariable String projectid, @RequestBody String body) {
		JsonResult result = JsonResult.parse(body);
		return JenkinsDao.save(projectid, result.read("folder").toString(), result.read("jobname").toString(), result.read("host").toString(), result.read("username").toString(), result.read("token").toString()).toResponse();
	}
	
	@RequestMapping("/build/{jobid}")
    public @ResponseBody String build(@PathVariable String jobid) {
//		TestJob job = ServiceJob.getJob(jobid);
//		TestJob excutableJob = ServiceJob.getAnExecutableJob(job.getProjectid());
//		JenkinsServer server = new JenkinsServer(job.getProjectid());
//		if (excutableJob != null && server.isBuildding()) {
//			excutableJob = JobDao.getJobByExcuteId(excutableJob.getExcuteid());
//			Response response = new Response();
//			response.setRetCode(1);
//			response.setRetMessage("有其它任务在执行");
//			return response.toString();
//		}
//		JobDao.addAnExcuteJob(job);
//		String number = server.build();
//		Response response = new Response();
//		Map<Object, Object> data = new HashMap<Object, Object>();
//		if (number != null && Integer.valueOf(number) > 0) {
//			data.put("value", number);
//			List<HashMap<Object, Object>> list = new ArrayList<HashMap<Object,Object>>();
//			list.add((HashMap<Object, Object>) data);
//			response.setList(list);
//			response.setRetMessage("构建成功");
//			JobDao.setJobBuildNumber(job, number);
//		}else {
//			response.setRetCodeFalse();
//			response.setRetMessage("构建失败");
//		}
//		return response.toString();
		return JobDao.addAnPenddingJobWithJobId(jobid).toResponse();
    }
	
	/**
	 * 查看allure报告
	 * @param buildNumber
	 * @return
	 */
	@RequestMapping(value = "/{jobid}/report/{buildNumber}", method=RequestMethod.GET)
	public String linkToAllureReport(@PathVariable String buildNumber, @PathVariable String jobid) {
		if ("no".equals(buildNumber)) {
			return "no";
		}
		JenkinsServer jenkins = new JenkinsServer(jobid);
		return "redirect:"+jenkins.getUrl()+"/job/"+jenkins.getJobName()+"/"+buildNumber+"/allure/";
	}
	
	/**
	 * 查看console输出
	 * @param buildNumber
	 * @return
	 */
	@RequestMapping(value = "/{jobid}/console/{buildNumber}", method=RequestMethod.GET)
	public String linkToBuildConsole(@PathVariable String buildNumber, @PathVariable String jobid) {
		if ("no".equals(buildNumber)) {
			return "no";
		}
		JenkinsServer jenkins = new JenkinsServer(jobid);
		return "redirect:"+jenkins.getUrl()+"/job/"+jenkins.getJobName()+"/"+buildNumber+"/console";
	}
	
	@RequestMapping("/{jobid}/build/{buildNumber}/status")
    public @ResponseBody String getBuildStatus(@PathVariable String jobid, @PathVariable Integer buildNumber) throws URISyntaxException, IOException{
		Response response = new Response();
		try {
			JenkinsServer server = new JenkinsServer(ServiceProject.getProjectFromJobId(jobid).getId());
			boolean temp = server.isBuildding(buildNumber);
			if (temp) {
				response.setRetMessage("正在构建");
			}else {
				response.setRetMessage("构建完成");
			}
		} catch (Exception e) {
			e.printStackTrace();
			response.setRetCode(0);
			response.setRetMessage("异常");
		}
		return response.toString();
    }
	
}
